home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 215_01.zip / BBSCFILE.C < prev    next >
Text File  |  1980-01-01  |  13KB  |  618 lines

  1. /*
  2.     bbscfile.c
  3.  
  4.     Support routines used by BBSc.c to do file i/o for the
  5.     message file.
  6.                 Mike Kelly
  7.  
  8.     06/12/83 v1.0    written
  9.     07/07/83 v1.0    updated
  10. */
  11.  
  12. #include "bdscio.h"
  13. #include "bbscdef.h"
  14.  
  15.  
  16. #define LASTDATE  " 07/07/83 "
  17.  
  18. #define PGMNAME "BBSCFILE "
  19. #define VERSION " 1.0 "
  20.  
  21.  
  22. /*    .    t.    e.    s.    t.    .    
  23.  
  24.  
  25. main(argc,argv)
  26. char    **argv;
  27. int    argc;
  28. {
  29.  
  30.     char    xtra[500];
  31.  
  32.     printf("starting %s\n",PGMNAME);
  33.     debug = 1;
  34.     strcpy(h_date,"01/01/83");
  35.     h_next = 1;
  36.  
  37.     hdrwrt();
  38.     hdrread();
  39.     strcpy(h_date,"02/01/83");
  40.     h_next = 4;
  41.     hdrwrt();
  42.     hdrread();
  43.     strcpy(xtra,".......160.......170.......180.......190.......200.......210.......220.......230.......240.......250.......260.......270.......280.......290");
  44.     strcpy(msg_text,"1.......10........20........30........40........50........60........70........80........90.......100.......110.......120.....128.........140.......150");
  45.     strcat(msg_text,xtra);
  46.     strcpy(msg_date,"01/01/83");
  47.     strcpy(msg_time,"01:01:10 PM");
  48.     strcpy(msg_to,"whom it may ");
  49.     strcpy(msg_from,"that guy");
  50.     strcpy(msg_pass,"abc");
  51.     strcpy(msg_subject,"this and that");
  52.     if (debug)
  53.     {
  54.         printf("after init, msg_text = ");
  55.         prthex(msg_text);
  56.         printf("\n");
  57.     }
  58.     h_next = 0;
  59.     msgwrt();
  60.     msgwrt();
  61.  
  62.     strfill(msg_text,0,800);
  63.     msgread(4);
  64.     printf("msg 4 = ");
  65.     prthex(msg_text);
  66.     printf("\n");
  67.  
  68.     msgread(0);
  69.     printf("msg 0 = ");
  70.     prthex(msg_text);
  71.     printf("\n");
  72.  
  73.     printf("ending %s\n",PGMNAME);
  74. }
  75.  
  76.     .    t.    e.    s.    t.    .    */
  77.  
  78.  
  79. hdrwrt()        /* write the header from memory variables */
  80. {            /* header is a 1 record file */
  81.     int    fd;
  82.     char    buf128[MSGSECT];
  83.  
  84.     if ((fd = open(HEADER,2)) == ERROR)    /* open i/o */
  85.     {
  86.         printf("can't open header.bbs will create it\n");
  87.         if ((fd = creat(HEADER)) == ERROR)
  88.         {
  89.             printf("can't create header.bbs - aborting\n");
  90.             return(ERROR);
  91.         }
  92.     }
  93.  
  94.     itoa(h_next_msg,h_next);    /* convert int to char */
  95.     strfill(buf128,26,MSGSECT);        /* init buf128 to all hex 1a */
  96.     sprintf(buf128,"%s~%s~",    /* build record */
  97.         h_next_msg,
  98.         h_date);
  99. #ifdef DEBUG
  100.     if (debug)
  101.     {
  102.         printf("after sprintf buf128 = ");
  103.         prthex(buf128);
  104.         printf("\n");
  105.     }
  106. #endif
  107.     write(fd,buf128,1);        /* write it */
  108.     close(fd);            /* no need to leave it open */
  109.     return(OK);
  110. }
  111.  
  112. hdrread()        /* read the header file into memory */
  113. {
  114.     int    fd,
  115.         cnt;
  116.     char    buf128[MSGSECT];
  117.  
  118.     if ((fd = open(HEADER,0)) == ERROR)    /* open input */
  119.     {
  120.         printf("can't open header.bbs \n");
  121.         h_next = 0;
  122.         h_next_msg[0] = '0';
  123.         h_date[0] = '0';
  124.         hdrwrt();
  125.     }
  126.     read(fd,buf128,1);
  127.     cnt = sscanf(buf128,"%s~%s~",
  128.             h_next_msg,
  129.             h_date);
  130. #ifdef DEBUG
  131.     if (debug)
  132.     {
  133.         printf("after sscanf buf128 = ");
  134.         prthex(buf128);
  135.         printf("\n");
  136.         printf("count returned from sscanf = %d\n",cnt);
  137.         printf("h_next_msg  = %s\n",h_next_msg);
  138.         printf("h_date = %s\n",h_date);
  139.     }
  140. #endif
  141.     close(fd);        /* no need to leave it open */
  142.  
  143.     if (cnt != 2)
  144.     {
  145.         return(ERROR);
  146.     }
  147.     h_next = atoi(h_next_msg);
  148.     return(OK);
  149. }
  150.  
  151. msgopen(how)
  152. int    how;        /* how to open 0=input, 1=output, 2=i/o */
  153. {
  154.     int    fd;
  155.  
  156.     if ((fd = open(MESSAGES,how)) == ERROR)    /* open i/o */
  157.     {
  158.         printf("can't open %s will create it\n",MESSAGES);
  159.         if ((fd = creat(MESSAGES)) == ERROR)
  160.         {
  161.             printf("can't create %s - aborting\n",MESSAGES);
  162.             return(ERROR);
  163.         }
  164.     }
  165.  
  166. #ifdef DEBUG
  167.     if (debug)
  168.     {
  169.         printf("in msgopen - fd = %d\n",fd);
  170.     }
  171. #endif
  172.     return(fd);
  173. }
  174.  
  175. msgclose(fd)
  176. int    fd;
  177. {
  178.  
  179. #ifdef DEBUG
  180.     if (debug)
  181.     {
  182.         printf("in msgclose - fd = %d\n",fd);
  183.     }
  184. #endif
  185.     return(close(fd));
  186. }
  187.  
  188. msgwrt(fd)        /* write the message file from memory variables */
  189. int    fd;        /* writes a message starting with the h_next msg # */
  190. {
  191.     int    rc,            /* return code */
  192.         cnt1,
  193.         cnt2,
  194.         len;
  195.     char    bufmsg0[MSG1MAX+1],
  196.         buf128[MSGSECT],
  197.         this1[10],
  198.         next1[10];
  199.  
  200.     rc = cnt1 = len = cnt2 = 0;
  201. #ifdef DEBUG
  202.     if (debug)
  203.     {
  204.         printf("in msgwrt\n");
  205.     }
  206. #endif
  207.     rc = seek(fd,h_next,0);        /* seek to next available sector */
  208. #ifdef DEBUG
  209.     if (debug)
  210.     {
  211.         printf("file open\n");
  212.         printf("seek = %d\n",rc);
  213.     }
  214. #endif
  215.     itoa(this1,h_next);        /* convert int to char */
  216.     h_next++;
  217.     itoa(next1,h_next);
  218.  
  219. #ifdef DEBUG
  220.     if (debug)
  221.     {
  222.         printf("before strfill of nulls\n");
  223.         printf("\n");
  224.     }
  225. #endif
  226.     strfill(buf128,0,MSGSECT);        /* init buf128 to all hex 00 */
  227.  
  228. /*
  229. *            build first piece of msg record
  230. */
  231.  
  232.     sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",    /* build record */
  233.         this1,                    /* this rcd # */
  234.         next1,                    /*  points to next rcd # */
  235.         msg_delete,                /* delete byte */
  236.         msg_date,
  237.         msg_time,
  238.         msg_to,
  239.         msg_from,
  240.         msg_pass,
  241.         msg_subject);
  242. #ifdef DEBUG
  243.     if (debug)
  244.     {
  245.         printf("before write of 1st buf128 = ");
  246.         prthex(buf128);
  247.         printf("\n");
  248.     }
  249. #endif
  250.     cnt1 = tell(fd);
  251. #ifdef DEBUG
  252.     if (debug)
  253.     {
  254.         printf("tell() value = %d\n",cnt1);
  255.     }
  256. #endif
  257.     rc = write(fd,buf128,1);    /* write the first 128 byte record */
  258.                     /*  for a message record */
  259. #ifdef DEBUG
  260.     if (debug)
  261.     {
  262.         printf("after write of 1st rcd return code = %d\n",rc);
  263.     }
  264. #endif
  265. /*
  266. *            build the n+1 piece of msg record
  267. */
  268.  
  269.     len = (strlen(msg_text) / MSG1MAX) + 1; /* calc how many more 128 */
  270.                         /*  byte records to write */
  271. #ifdef DEBUG
  272.     if (debug)
  273.     {
  274.         printf("length of msg_text/128 + 1 = %d\n",len);
  275.     }
  276. #endif
  277.     cnt2 = 1;            /* init for substr */
  278.     while (len--)
  279.     {
  280.         itoa(this1,h_next);        /* calc/convert record #'s */
  281.         h_next++;
  282.         if (len == 0)
  283.         {
  284.             strcpy(next1,"0");    /* marks last 128 byte piece */
  285.         }                /*  of a msg */
  286.         else
  287.         {
  288.             itoa(next1,h_next);
  289.         }
  290.         strfill(bufmsg0,0,MSG1MAX);
  291.         substr(msg_text,bufmsg0,cnt2,MSG1MAX); /* move MSG1MAX bytes to buffer */
  292. #ifdef DEBUG
  293.         if (debug)
  294.         {
  295.             printf("after substr bufmsg0 = ");
  296.             prthex(bufmsg0);
  297.             printf("\n");
  298.         }
  299. #endif
  300.         cnt2 += MSG1MAX;        /* up cnt2 by MSG1MAX */
  301. #ifdef DEBUG
  302.         if (debug)
  303.         {
  304.             printf("after add cnt2 = %d\n",cnt2);
  305.         }
  306. #endif
  307.         strfill(buf128,0,MSGSECT);    /* init buf128 to all hex 00 */
  308.         sprintf(buf128,"%s~%s~%s~%s~",
  309.             this1,            /* this rcd # */
  310.             next1,            /* point to next rcd # */
  311.             msg_delete,        /* delete byte */
  312.             bufmsg0);        /* piece of msg */
  313. #ifdef DEBUG
  314.         if (debug)
  315.         {
  316.             printf("after sprintf, msg_text = ");
  317.             prthex(msg_text);
  318.             printf("\n");
  319.             printf("after sprintf, buf128 = ");
  320.             prthex(buf128);
  321.             printf("\n");
  322.             printf("after sprintf, bufmsg0 = ");
  323.             prthex(bufmsg0);
  324.             printf("\n");
  325.         }
  326. #endif
  327.         cnt1 = tell(fd);
  328. #ifdef DEBUG
  329.         if (debug)
  330.         {
  331.             printf("tell() value = %d\n",cnt1);
  332.         }
  333. #endif
  334.         rc = write(fd,buf128,1);    /* write the n+1 128 byte record */
  335. #ifdef DEBUG
  336.         if (debug)
  337.         {
  338.             printf("after write of n+1 return code = %d\n",rc);
  339.             printf("after write of n+1 buf128 = ");
  340.             prthex(buf128);
  341.             printf("\n");
  342.         }
  343. #endif
  344.     }
  345.  
  346.     strfill(buf128,26,MSGSECT);        /* fill with all hex 1a */
  347.     cnt1 = tell(fd);
  348. #ifdef DEBUG
  349.     if (debug)
  350.     {
  351.         printf("tell() value = %d\n",cnt1);
  352.     }
  353. #endif
  354.     rc = write(fd,buf128,1);    /* write the all hex 1a 128 byte record */
  355. #ifdef DEBUG
  356.     if (debug)
  357.     {
  358.         printf("after write of all hex 1a return code = %d\n",rc);
  359.         printf("after write of hex 1a buf128 = ");
  360.         prthex(buf128);
  361.         printf("\n");
  362.         printf("leaving msgwrt\n");
  363.     }
  364. #endif
  365.     return(OK);
  366. }
  367.  
  368. msgrewrt(fd,r_msg)    /* re-write the message file from memory variables */
  369. int    fd,        /* re-writes only the 1st part of a message */
  370.     r_msg;        /* used to update the delete byte */
  371. {
  372.     int    rc,            /* return code */
  373.         cnt1,
  374.         file_size;
  375.     char    buf128[MSGSECT],
  376.         this1[10],
  377.         next1[10];
  378.  
  379.     rc = cnt1 = 0;
  380. #ifdef DEBUG
  381.     if (debug)
  382.     {
  383.         printf("in msgrewrt\n");
  384.         printf("rewrite msg = %d\n",r_msg);
  385.     }
  386. #endif
  387.     file_size = rcfsiz(fd);        /* find how many 128 byte sectors */
  388. #ifdef DEBUG
  389.     if (debug)            /*  are in the file and don't seek */
  390.     {                /*  past that */ 
  391.         printf("file_size = %d<%04x>\n",file_size,file_size);
  392.     }
  393. #endif
  394.     if (r_msg >= (file_size - 1))    /* don't try to seek past end of file */
  395.     {
  396.         return(ERROR);
  397.     }
  398.     if ((rc = seek(fd,r_msg,0)) == ERROR)    /* seek to requested sector */
  399.     {
  400.         return(ERROR);
  401.     }
  402. #ifdef DEBUG
  403.     if (debug)
  404.     {
  405.         printf("file open\n");
  406.         printf("seek = %d\n",rc);
  407.     }
  408. #endif
  409.     itoa(this1,r_msg);        /* convert int to char */
  410.     r_msg++;
  411.     itoa(next1,r_msg);
  412. #ifdef DEBUG
  413.     if (debug)
  414.     {
  415.         printf("before strfill of nulls\n");
  416.         printf("\n");
  417.     }
  418. #endif
  419.     strfill(buf128,0,MSGSECT);        /* init buf128 to all hex 00 */
  420.  
  421. /*
  422. *            build first piece of msg record
  423. */
  424.  
  425.     sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",    /* build record */
  426.         this1,                    /* this rcd # */
  427.         next1,                    /*  points to next rcd # */
  428.         msg_delete,                /* delete byte */
  429.         msg_date,
  430.         msg_time,
  431.         msg_to,
  432.         msg_from,
  433.         msg_pass,
  434.         msg_subject);
  435. #ifdef DEBUG
  436.     if (debug)
  437.     {
  438.         printf("before re-write of 1st buf128 = ");
  439.         prthex(buf128);
  440.         printf("\n");
  441.         cnt1 = tell(fd);
  442.         printf("tell() value = %d\n",cnt1);
  443.     }
  444. #endif
  445.     rc = write(fd,buf128,1);    /* write the first 128 byte record */
  446.                     /*  for a message record */
  447. #ifdef DEBUG
  448.     if (debug)
  449.     {
  450.         printf("after rewrite of 1st rcd return code = %d\n",rc);
  451.     }
  452. #endif
  453.     return(OK);
  454. }
  455.  
  456.  
  457. msgread(fd,msgno)        /* read message number requested */
  458. int    fd,            /* returns ERROR if msg past eof */
  459.     msgno;            /* returns 0 if msg is not 1st piece */
  460.                 /*   of a message */
  461.                 /* returns 0 if msg is deleted */
  462.                 /* returns msg # if successful */
  463. {
  464.     int    rc,            /* return code */
  465.         cnt1,
  466.         cnt2,
  467.         len,
  468.         next,
  469.         ret_this,
  470.         file_size;
  471.     char    bufmsg0[MSG1MAX+1],
  472.         buf128[MSGSECT+256],
  473.         this1[10],
  474.         next1[10];
  475.  
  476. #ifdef DEBUG
  477.     if (debug)
  478.     {
  479.         printf("in msgread-message to read = %d\n",msgno);
  480.         printf("fd passed = %d\n",fd);
  481.     }
  482. #endif
  483.     file_size = rcfsiz(fd);        /* find how many 128 byte sectors */
  484. #ifdef DEBUG
  485.     if (debug)            /*  are in the file and don't seek */
  486.     {                /*  past that */ 
  487.         printf("file_size = %d<%04x>\n",file_size,file_size);
  488.     }
  489. #endif
  490.     if (msgno >= (file_size - 1))    /* don't try to seek past end of file */
  491.     {
  492.         return(ERROR);
  493.     }
  494.     if ((rc = seek(fd,msgno,0)) == ERROR)
  495.     {
  496.         printf("can't seek on %s\n",MESSAGES);
  497.         printf("rc on seek to record %d = %d<%04x>\n",msgno,rc,rc);
  498.         return(ERROR);        /* when cant find it */
  499.     }
  500. #ifdef DEBUG
  501.     if (debug)
  502.     {
  503.         printf("after seek of 1st sector, seek = %d\n",rc);
  504.     }
  505. #endif
  506.     if ((rc = read(fd,buf128,1)) == ERROR)    /* read one 128 byte sector */
  507.     {
  508.         printf("can't read %s\n",MESSAGES);
  509.         printf("rc on read to 1st record %d = %d<%04x>\n",
  510.             msgno,rc,rc);
  511.         return(ERROR);
  512.     }
  513. #ifdef DEBUG
  514.     if (debug)
  515.     {
  516.         printf("after read rc = %d\n",rc);
  517.     }
  518. #endif
  519. /*
  520. *            get first piece of msg record
  521. */
  522.     rc = sscanf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",
  523.         this1,                /* this rcd # */
  524.         next1,                /*  points to next rcd # */
  525.         msg_delete,            /* delete byte */
  526.         msg_date,
  527.         msg_time,
  528.         msg_to,
  529.         msg_from,
  530.         msg_pass,
  531.         msg_subject);
  532. #ifdef DEBUG
  533.     if (debug)
  534.     {
  535.         printf("after sscanf of 1st piece - rc = %d\n",rc);
  536.         printf("msg_delete = ");
  537.         prthex(msg_delete);
  538.         printf("\n");
  539.     }
  540. #endif
  541.     if (rc != 9)        /* makes sure we read the 1st piece */
  542.     {            /*  of a message and not in the middle */
  543. #ifdef DEBUG
  544.         if (debug)
  545.         {
  546.             printf("rc != 9\n");
  547.         }
  548. #endif
  549.         return(0);    /* 0 when is not the msg header */
  550.     }
  551.  
  552.     if (msg_delete[0] == '9')    /* check for deleted messages */
  553.     {                /*  if so, return as if not found */
  554. #ifdef DEBUG
  555.         if (debug)
  556.         {
  557.             printf("msg_delete[0] = 9\n");
  558.         }
  559. #endif
  560.         return(0);
  561.     }
  562.  
  563.     ret_this = atoi(this1);    /* return this msg no. */
  564.     next = atoi(next1);
  565.     strcpy(msg_no,this1);
  566.     msg_text[0] = '\0';
  567. #ifdef DEBUG
  568.     if (debug)
  569.     {
  570.         printf("after sscanf of 1st sector, buf128 = ");
  571.         prthex(buf128);
  572.         printf("\n");
  573.         printf("this1 = %s   next1 = %s\n",this1,next1);
  574.         printf("after atoi, next = %d\n",next);
  575.     }
  576. #endif
  577.     while (next)            /* read until no more pieces for */
  578.     {                /*  this message */
  579.         if ((rc = read(fd,buf128,1)) == ERROR)    /* read next 128 byte sector */
  580.         {
  581.             printf("can't read %s\n",MESSAGES);
  582.             printf("rc on n+1 read to record %d = %d<%04x>\n",
  583.                 msgno,rc,rc);
  584.             return(ERROR);
  585.         }
  586. #ifdef DEBUG
  587.         if (debug)
  588.         {
  589.             printf("rc on n+1 read to record %d = %d<%04x>\n",
  590.                 msgno,rc,rc);
  591.         }
  592. #endif
  593.         strfill(bufmsg0,0,MSG1MAX);    /* init bufmsg0 to all hex 00 */
  594.         rc = sscanf(buf128,"%s~%s~%s~%s~",
  595.             this1,            /* this rcd # */
  596.             next1,            /* point to next rcd # */
  597.             msg_delete,        /* delete byte */
  598.             bufmsg0);        /* piece of msg */
  599.         next = atoi(next1);
  600. #ifdef DEBUG
  601.         if (debug)
  602.         {
  603.             printf("return from sscanf = %d\n",rc);
  604.             printf("after sscanf of n+1, buf128 = ");
  605.             prthex(buf128);
  606.             printf("\n");
  607.             printf("this1=%s  next1=%s  next=%d\n",
  608.                 this1,next1,next);
  609.         }
  610. #endif
  611.         strcat(msg_text,bufmsg0);    /* tag piece of msg to */
  612.                         /*  whole msg array */
  613.     }
  614.     return(ret_this);    /* if all ok, return the msg no. found */
  615. }
  616.  
  617. /*    end of program      */
  618.